'''
csv文件，内容为电话簿
人名,电话号码,地址
功能：电话簿的增删改查
5个功能分别放在5个函数中
'''
import csv
# 查看所有信息
def listphones():
    with open("phone.csv", "r", encoding="utf-8") as f:
        reader1 = csv.DictReader(f)
        for row in reader1:  # row是一行数据，类型为字典
            print( row.get("姓名"), '：', row["电话号码"],"\t",row["地址"])

# 查看单个电话信息
def showphone(name):
    with open("phone.csv", "r", encoding="utf-8") as f:
        reader2 = csv.DictReader(f)
        for row in reader2:  # row是一行数据，类型为字典
            if row.get("姓名")==name:
                print(row.get("姓名"), '：', row["电话号码"]," ",row["地址"])


# 根据姓名删除信息

def delete(name):
    # 空列表
    a = []
    # 边读边往列表里面添加
    # 如果是删除的name 就不添加
    with open("phone.csv", "r", encoding="utf-8") as f:
        reader3 = csv.DictReader(f)
        # 保存表头信息
        headers = reader3.fieldnames
        for row in reader3:  # row是一行数据，类型为字典
            if row.get("姓名") != name:
                a.append([row.get("姓名"),row.get("电话号码"),row.get("地址")])
    with open("phone.csv", "w", encoding="utf-8", newline="") as f:
        writer1 = csv.writer(f)
        # 先写入表头
        writer1.writerow(headers)
        # 再写入所有保留的数据
        writer1.writerows(a)
    # 写  将列表中的数据覆盖到文件中


# 修改信息根据姓名修改信息
def change(name,new_name, new_phone, new_address):
        # 空列表
        # 如果是删除的name 就不添加
        b = []
        with open("phone.csv", "r", encoding="utf-8") as f:
            # 边读边往列表里面添加
            reader4 = csv.DictReader(f)
            # 保存表头信息
            headers = reader4.fieldnames
            for row in reader4:  # row是一行数据，类型为字典
                if row.get("姓名") == name:
                    b.append([new_name, new_phone, new_address])
                else:
                    b.append([row.get("姓名"), row.get("电话号码"), row.get("地址")])
        with open("phone.csv", "w", encoding="utf-8", newline="") as f:
            writer2 = csv.writer(f)
            # 先写入表头
            writer2.writerow(headers)
            writer2.writerows(b)
            # 再写入所有保留的数据

        # 写  将列表中的数据覆盖到文件中

# 添加
def add(name,phone,address):
    with open("phone.csv", "a", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=["姓名", "电话号码","地址"])
        w.writerow({"姓名": name, "电话号码": phone,"地址":address})

def main():
    print("------欢迎进入电话簿系统--------")

    while True:

        print("------1 查看所有电话信息--------")
        print("------2 查看单个电话信息--------")
        print("------3 根据姓名删除信息--------")
        print("------4 根据姓名修改信息--------")
        print("------5 添加电话信息   --------")
        print("------6 退出系统      --------")
        num = input("------请输入你的操作--------\n")
        if num == '1':
            listphones()
        elif num == '2':
            name  = input("请输入需要查看的姓名：")
            showphone(name)
        elif num == '3':
            name = input("请输入需要删除的姓名：")
            delete(name)

        elif num == '4':
            name = input("请输入需要修改的姓名：")
            new_name = input("请输入修改后的姓名：")
            new_phone = input("电话号码：")
            new_address = input("地址：")
            change(name,new_name, new_phone, new_address)
            # change()
        elif num == '5':
            # 添加电话信息
            name = input("请输入需要添加的姓名：")
            phone = input("电话号码：")
            address = input("地址：")
            add(name,phone,address)
        else:
            # 退出循环
            break

main()